﻿#ifndef QZIP_H
#define QZIP_H
#include <QString>
#include <QFile>
#include <QDateTime>
class QZipWriterPrivate;
class QZipReaderPrivate;

#ifdef WIN32
	#ifdef BUILD_QZIP_LIB
		#define QZIP_EXPORT __declspec(dllexport)
	#else
		#define QZIP_EXPORT __declspec(dllimport)
	#endif
#else
	#define QZIP_EXPORT
#endif

class QZIP_EXPORT QZipWriter
{
public:
    explicit QZipWriter(const QString &fileName, QIODevice::OpenMode mode = (QIODevice::WriteOnly | QIODevice::Truncate) );
    explicit QZipWriter(QIODevice *device);
    ~QZipWriter();

    QIODevice* device() const;
    bool isWritable() const;
    bool exists() const;

    enum Status {
        NoError,
        FileWriteError,
        FileOpenError,
        FilePermissionsError,
        FileError
    };

    Status status() const;

    enum CompressionPolicy {
        AlwaysCompress,
        NeverCompress,
        AutoCompress
    };

    void setCompressionPolicy(CompressionPolicy policy);
    CompressionPolicy compressionPolicy() const;

    void setCreationPermissions(QFile::Permissions permissions);
    QFile::Permissions creationPermissions() const;

    void addFile(const QString &fileName, const QByteArray &data);
    void addFile(const QString &fileName, QIODevice *device);
    void addDirectory(const QString &dirName);
    void addSymLink(const QString &fileName, const QString &destination);
    void close();
	
private:
    QZipWriterPrivate *d;
    Q_DISABLE_COPY(QZipWriter)
};

class QZIP_EXPORT QZipReader
{
public:
	explicit QZipReader(const QString &fileName, QIODevice::OpenMode mode = QIODevice::ReadOnly);
    explicit QZipReader(QIODevice *device);
    ~QZipReader();

    QIODevice* device() const;
    bool isReadable() const;
    bool exists() const;

	struct QZIP_EXPORT FileInfo
    {
        FileInfo();
        FileInfo(const FileInfo &other);
        ~FileInfo();
        FileInfo &operator=(const FileInfo &other);
        bool isValid() const;
        QString filePath;
        uint isDir : 1;
        uint isFile : 1;
        uint isSymLink : 1;
        QFile::Permissions permissions;
        uint crc;
        qint64 size;
        QDateTime lastModified;
        void *d;
    };

    QList<FileInfo> fileInfoList() const;
    int count() const;

    FileInfo entryInfoAt(int index) const;
    QByteArray fileData(const QString &fileName) const;
    bool extractAll(const QString &destinationDir) const;

    enum Status {
        NoError,
        FileReadError,
        FileOpenError,
        FilePermissionsError,
        FileError
    };

    Status status() const;
    void close();

private:
    QZipReaderPrivate *d;
    Q_DISABLE_COPY(QZipReader)
};

#endif // QZIP_H
